<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>动植物图库 - 服务器资源</title>
    <style>
        :root {
            --primary-color: #4a8c2b;
            --secondary-color: #f5f7fa;
            --text-color: #333;
        }
        
        body {
            font-family: 'Segoe UI', Ubuntu, sans-serif;
            line-height: 1.6;
            color: var(--text-color);
            background-color: #f0f2f5;
            margin: 0;
            padding: 20px;
        }
        
        .container {
            max-width: 1200px;
            margin: 0 auto;
            background: white;
            border-radius: 10px;
            box-shadow: 0 0 20px rgba(0,0,0,0.1);
            overflow: hidden;
        }
        
        header {
            background: var(--primary-color);
            color: white;
            padding: 20px 30px;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }
        
        h1 {
            margin: 0;
            font-size: 1.8rem;
        }
        
        .upload-btn {
            background: white;
            color: var(--primary-color);
            border: none;
            padding: 8px 16px;
            border-radius: 4px;
            cursor: pointer;
            font-weight: bold;
            transition: all 0.3s;
        }
        
        .upload-btn:hover {
            background: #e0e0e0;
        }
        
        .gallery {
            display: grid;
            grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
            gap: 20px;
            padding: 20px;
        }
        
        .gallery-item {
            border-radius: 8px;
            overflow: hidden;
            box-shadow: 0 3px 10px rgba(0,0,0,0.1);
            transition: transform 0.3s;
            background: var(--secondary-color);
            position: relative;
        }
        
        .gallery-item:hover {
            transform: translateY(-5px);
        }
        
        .gallery-img {
            width: 100%;
            height: 180px;
            object-fit: cover;
            border-bottom: 1px solid #ddd;
            cursor: pointer;
        }
        
        .gallery-info {
            padding: 12px;
        }
        
        .gallery-name {
            font-weight: bold;
            margin-bottom: 5px;
            white-space: nowrap;
            overflow: hidden;
            text-overflow: ellipsis;
        }
        
        .gallery-meta {
            display: flex;
            justify-content: space-between;
            font-size: 0.8rem;
            color: #666;
        }
        
        .server-info {
            text-align: center;
            padding: 15px;
            font-size: 0.9rem;
            color: #777;
            border-top: 1px solid #eee;
        }
        
        /* 加载状态 */
        .loading {
            text-align: center;
            padding: 40px;
            grid-column: 1 / -1;
            color: var(--primary-color);
        }
        
        .spinner {
            border: 4px solid rgba(0,0,0,0.1);
            border-radius: 50%;
            border-top: 4px solid var(--primary-color);
            width: 40px;
            height: 40px;
            animation: spin 1s linear infinite;
            margin: 0 auto 15px;
        }
        
        @keyframes spin {
            0% { transform: rotate(0deg); }
            100% { transform: rotate(360deg); }
        }
        
        .error {
            color: #d32f2f;
            text-align: center;
            padding: 40px;
            grid-column: 1 / -1;
        }
        
        /* 响应式设计 */
        @media (max-width: 768px) {
            .gallery {
                grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <header>
            <h1>🐾 动植物图片资源库</h1>
            <button class="upload-btn" id="uploadBtn">上传图片</button>
        </header>
        
        <div class="gallery" id="galleryContainer">
            <div class="loading" id="loadingIndicator">
                <div class="spinner"></div>
                <p>正在加载图片...</p>
            </div>
        </div>
        
        <div class="server-info">
            Apache/2.4.52 (Ubuntu) Server at 8.138.215.14 Port 80
            <div id="lastUpdate"></div>
        </div>
    </div>

    <!-- 图片预览模态框 -->
    <div id="imageModal" style="display:none; position:fixed; top:0; left:0; width:100%; height:100%; background:rgba(0,0,0,0.8); z-index:1000; justify-content:center; align-items:center;">
        <img id="modalImage" style="max-height:90%; max-width:90%; object-fit:contain;">
        <button id="closeModal" style="position:absolute; top:20px; right:20px; background:#fff; border:none; border-radius:50%; width:40px; height:40px; font-size:20px; cursor:pointer;">×</button>
    </div>

    <script>
        // DOM元素
        const galleryContainer = document.getElementById('galleryContainer');
        const loadingIndicator = document.getElementById('loadingIndicator');
        const uploadBtn = document.getElementById('uploadBtn');
        const lastUpdateEl = document.getElementById('lastUpdate');
        const imageModal = document.getElementById('imageModal');
        const modalImage = document.getElementById('modalImage');
        const closeModal = document.getElementById('closeModal');

        // 初始化函数
        async function init() {
            await loadImages();
            
            // 设置定时刷新（每2分钟）
            setInterval(loadImages, 120000);
            
            // 更新最后更新时间
            updateLastUpdateTime();
        }

        // 加载图片
        async function loadImages() {
            try {
                showLoading();
                
                const response = await fetch('get_images.php');
                const data = await response.json();
                
                if (!data.success) {
                    throw new Error(data.message || '获取图片列表失败');
                }
                
                renderGallery(data.images);
                updateLastUpdateTime();
            } catch (error) {
                showError(error.message);
                console.error('加载图片失败:', error);
            }
        }

        // 渲染画廊
        function renderGallery(images) {
            if (!images || images.length === 0) {
                galleryContainer.innerHTML = '<div class="loading">暂无图片</div>';
                return;
            }
            
            let html = '';
            images.forEach(image => {
                html += `
                    <div class="gallery-item">
                        <img src="${image.url}" 
                             class="gallery-img" 
                             alt="${image.alt}"
                             loading="lazy"
                             data-full="${image.url}">
                        <div class="gallery-info">
                            <div class="gallery-name">${image.name}</div>
                            <div class="gallery-meta">
                                <span>${image.size}</span>
                                <span>${image.date}</span>
                            </div>
                        </div>
                    </div>
                `;
            });
            
            galleryContainer.innerHTML = html;
            
            // 添加图片点击事件
            document.querySelectorAll('.gallery-img').forEach(img => {
                img.addEventListener('click', () => {
                    modalImage.src = img.dataset.full;
                    imageModal.style.display = 'flex';
                });
            });
        }

        // 上传功能
        uploadBtn.addEventListener('click', async () => {
            const input = document.createElement('input');
            input.type = 'file';
            input.accept = 'image/*';
            input.multiple = true;
            
            input.onchange = async (e) => {
                if (e.target.files.length > 0) {
                    try {
                        showLoading('正在上传图片...');
                        
                        const formData = new FormData();
                        for (let i = 0; i < e.target.files.length; i++) {
                            formData.append('images[]', e.target.files[i]);
                        }
                        
                        const response = await fetch('upload.php', {
                            method: 'POST',
                            body: formData
                        });
                        
                        const result = await response.json();
                        
                        if (!result.success) {
                            throw new Error(result.message || '上传失败');
                        }
                        
                        alert(`成功上传 ${result.uploadedCount} 张图片`);
                        await loadImages(); // 刷新图片列表
                    } catch (error) {
                        alert('上传失败: ' + error.message);
                        console.error('上传错误:', error);
                    }
                }
            };
            
            input.click();
        });

        // 显示加载状态
        function showLoading(message = '正在加载图片...') {
            loadingIndicator.innerHTML = `
                <div class="spinner"></div>
                <p>${message}</p>
            `;
            loadingIndicator.style.display = 'block';
            galleryContainer.innerHTML = '';
            galleryContainer.appendChild(loadingIndicator);
        }

        // 显示错误
        function showError(message) {
            galleryContainer.innerHTML = `
                <div class="error">
                    <p>${message}</p>
                    <button onclick="location.reload()" style="margin-top:15px; padding:8px 16px; background:var(--primary-color); color:white; border:none; border-radius:4px; cursor:pointer;">
                        重新加载
                    </button>
                </div>
            `;
        }

        // 更新最后更新时间
        function updateLastUpdateTime() {
            const now = new Date();
            lastUpdateEl.textContent = `最后更新: ${now.toLocaleString()}`;
        }

        // 图片预览功能
        closeModal.addEventListener('click', () => {
            imageModal.style.display = 'none';
        });

        imageModal.addEventListener('click', (e) => {
            if (e.target === imageModal) {
                imageModal.style.display = 'none';
            }
        });

        // 初始化
        document.addEventListener('DOMContentLoaded', init);
    </script>
</body>
</html>